using IM.Easy.Infrastructure.Query;
using Microsoft.AspNetCore.Mvc;
using IM.Easy.ScheduledServices.IServices;
using IM.Easy.ScheduledServices.Dtos;
using IM.Easy.Entity.Scheduled;
using IM.Easy.Core.Attributes;

namespace IM.Easy.Api.Controllers.Log
{
    /// <summary>
    /// 定时作业日志 控制器
    /// </summary>
    [Route("log/jobexecutionhistory")]
    public class JobExecutionHistoryController : ControllerBase
    {
        private readonly IJobExecutionHistoryService _service;

        /// <summary>
        /// 构造函数，注入服务
        /// </summary>
        /// <param name="service">服务接口</param>
        public JobExecutionHistoryController(IJobExecutionHistoryService service)
        {
            _service = service;
        }

        /// <summary>
        /// 获取所有JobExecutionHistory对象
        /// </summary>
        /// <returns>返回JobExecutionHistory列表</returns>
        [HttpGet]
        [Api(Name = "获取定时作业日志列表", Perm = "job:log:list")]
        public PageResponse<JobExecutionHistoryDto> GetList([FromQuery] JobExecutionHistoryQueryDto query)
        {
            return _service.GetPageList(query);
        }

        /// <summary>
        /// 根据ID获取JobExecutionHistory对象
        /// </summary>
        /// <param name="id">对象的ID</param>
        /// <returns>返回指定的JobExecutionHistory对象</returns>
        [HttpGet("{id}")]
        [Api(Name = "获取定时作业日志明细", Perm = "job:log:info")]
        public JobExecutionHistory GetById([FromRoute] Guid id)
        {
            var result = _service.GetById(id);
            return result ?? throw new KeyNotFoundException($"未找到ID为 {id} 的JobExecutionHistory对象。");
        }

        /// <summary>
        /// 批量删除JobExecutionHistory对象
        /// </summary>
        /// <param name="ids">对象的IDS</param>
        [HttpDelete]
        [Api(Name = "删除定时作业日志", Perm = "job:log:del")]
        public void Delete([FromBody] Guid[] ids)
        {
            _service.Delete(ids);
        }

        /// <summary>
        /// 一键清空所有
        /// </summary>
        /// <returns></returns>
        [HttpDelete("clear")]
        [Api(Name = "作业日志一键清空", Perm = "job:del:all")]
        public bool ClearAll()
        {
            return _service.Delete(s => true);
        }
    }
}
